package com.example.controller;

import com.example.dto.OrderCreateDTO;
import com.example.dto.OrderResponseDTO;
import com.example.entity.Order;
import com.example.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/orders")
@RequiredArgsConstructor
@Tag(name = "订单管理", description = "订单创建、查询接口")
public class OrderController {

    private final OrderService orderService;

    /**
     * 创建订单（触发 Camunda 流程）
     */
    @PostMapping
    @Operation(summary = "创建订单", description = "创建订单并启动订单流转流程")
    public ResponseEntity<OrderResponseDTO> createOrder(@Validated @RequestBody OrderCreateDTO createDTO) {
        Order order = orderService.createOrder(createDTO);
        return ResponseEntity.ok(OrderResponseDTO.fromEntity(order));
    }

    /**
     * 根据订单编号查询订单
     */
    @GetMapping("/{orderNo}")
    @Operation(summary = "查询订单", description = "根据订单编号查询订单状态及详情")
    public ResponseEntity<OrderResponseDTO> getOrder(
            @Parameter(description = "订单编号") @PathVariable String orderNo) {
        Order order = orderService.getOrderByNo(orderNo);
        return ResponseEntity.ok(OrderResponseDTO.fromEntity(order));
    }
}